package fr.buildcity.jeu.map;

import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Shape;
import org.newdawn.slick.geom.Vector2f;
import org.newdawn.slick.gui.AbstractComponent;
import org.newdawn.slick.gui.ComponentListener;
import org.newdawn.slick.gui.GUIContext;

import fr.buildcity.enumerations.TypeCase;

public class Case extends AbstractComponent {

	private boolean over = false;
	private boolean mouseDown = false;

	/** The shape defining the area */
	private Shape area;
	private Color current;

	private TypeCase typeCase;

	private Vector2f posEcran = null;

	/*** par la suite cela dépendra de la taille d'une image ***/
	public static float CASE_LARGEUR = 21.5f;
	public static float CASE_HAUTEUR = 15f;

	public Case(GUIContext container, Shape shape, ComponentListener listener,
			Vector2f posEcran) {
		super(container);

		addListener(listener);
		this.posEcran = posEcran;

		Input input = container.getInput();

		int r = (int) (Math.random() * 3);

		switch (r) {
		case 0:
			typeCase = TypeCase.PLAINE;
			break;
		case 1:
			typeCase = TypeCase.MER;
			break;
		case 2:
			typeCase = TypeCase.INCONSTRUCTIBLE;
			break;
		default:
			typeCase = TypeCase.PLAINE;
			break;
		}

		current = typeCase.getColor();

		area = shape;
		over = area.contains(input.getMouseX(), input.getMouseY());
		mouseDown = input.isMouseButtonDown(0);
	}

	/**
	 * Update the current normalImage based on the mouse state
	 */
	private void updateImage(Graphics g) {
		if (over && mouseDown) {
			notifyListeners();
		}
	}

	@Override
	public void render(GUIContext arg0, Graphics g) throws SlickException {
		g.setColor(current);
		g.fill(area);
		updateImage(g);
	}

	/**
	 * @see org.newdawn.slick.util.InputAdapter#mouseMoved(int, int, int, int)
	 */
	public void mouseMoved(int oldx, int oldy, int newx, int newy) {
		over = area.contains(newx, newy);
	}

	/**
	 * @see org.newdawn.slick.util.InputAdapter#mousePressed(int, int, int)
	 */
	public void mousePressed(int button, int mx, int my) {
		over = area.contains(mx, my);
		if (button == 0) {
			mouseDown = true;
		}
	}

	/**
	 * @see org.newdawn.slick.util.InputAdapter#mouseReleased(int, int, int)
	 */
	public void mouseReleased(int button, int mx, int my) {
		over = area.contains(mx, my);
		if (button == 0) {
			mouseDown = false;
		}
	}

	@Override
	public void setLocation(int arg0, int arg1) {
	}

	@Override
	public int getHeight() {
		return 0;
	}

	@Override
	public int getWidth() {
		return 0;
	}

	@Override
	public int getX() {
		return 0;
	}

	@Override
	public int getY() {
		return 0;
	}

	public boolean isOver() {
		return over;
	}

	public void setOver(boolean over) {
		this.over = over;
	}

	public boolean isMouseDown() {
		return mouseDown;
	}

	public void setMouseDown(boolean mouseDown) {
		this.mouseDown = mouseDown;
	}

	public Vector2f getPosEcran() {
		return posEcran;
	}

	public void setPosEcran(Vector2f posEcran) {
		this.posEcran = posEcran;
	}

	public String toString() {
		return posEcran.getX() + "," + posEcran.getY();
	}
}
